using System;
using System.Collections.Generic;
using System.Text;
using DataAccess;
using Task.Contracts;
using Globals;

namespace Task
{
    public class GalleryTask : IGalleryTask
    {
        public void AddGallery(int userId, string galleryName, string galleryDescription)
        {
            NHibernateDao<gallery, int> galleryFactory = new NHibernateDao<gallery, int>();
            gallery newGallery = new gallery();
            newGallery.user_id = userId;
            newGallery.name = galleryName.Trim();
            newGallery.description = galleryDescription.Trim();

            NHibernateDao<user, int> userFactory = new NHibernateDao<user, int>();
            user tmpUser = userFactory.GetById(userId, false);
            newGallery.user = tmpUser; 

            galleryFactory.Save(newGallery);
        }

        public List<gallery> ReadGalleriesByUserId(int userId)
        {
            NHibernateDao<gallery, int> factory = new NHibernateDao<gallery, int>();
            List<gallery> lista
                = factory.GetByCriteria(NHibernate.Expression.Expression.Eq("user_id", userId));
            return lista;
        }
        
        public List<gallery> ReadGalleriesByUserIdMaxNumber(int userId)
        {
            int maxGalleries = 4;
            NHibernateDao<gallery, int> factory = new NHibernateDao<gallery, int>();
            NHibernate.Expression.ICriterion criterion =
                NHibernate.Expression.Expression.Eq("user_id", userId);
            List<gallery> lista = factory.GetByCriteria(0, maxGalleries, "id", false, criterion);
            return lista;
        }
        


        public gallery ReadGalleryByGalleryId(int galleryId)
        {
            NHibernateDao<gallery, int> factory = new NHibernateDao<gallery, int>();
            List<gallery> lista
                = factory.GetByCriteria(NHibernate.Expression.Expression.Eq("id", galleryId));
            if (lista != null)
            {
                foreach (gallery g in lista)
                {
                    return g;
                }
            }
            return null;
        }

        public user GetUserByGalleryId(int galleryId)
        {
            NHibernateDao<gallery, int> factory = new NHibernateDao<gallery, int>();
            gallery galleria = factory.GetById(galleryId, false);
            return galleria.user;
        }


        public List<art> ReadAllArtForUserForGalleryForType(int idUser, int idGallery, int artTypeId)
        {
            NHibernate.Expression.ICriterion criterion =
                NHibernate.Expression.Expression.And
                (
                    NHibernate.Expression.Expression.Eq("art_type_id", artTypeId)
                    ,
                    NHibernate.Expression.Expression.And
                    (
                        
                        NHibernate.Expression.Expression.Eq("user_id", idUser)
                        ,
                        NHibernate.Expression.Expression.Eq("gallery_id", idGallery)
                    )  
                );

            NHibernateDao<art, int> artFactory = new NHibernateDao<art, int>();
            List<art> arts = artFactory.GetByCriteria(0, 1000, "id", false, criterion);

            return arts;
        }

        public List<art> ReadArtByGalleryId(int galleryId, int start, int howMany, ContentNamesGlobals.ArtType typ)
        {
            NHibernate.Expression.ICriterion criterion =
                NHibernate.Expression.Expression.And
                (
                    NHibernate.Expression.Expression.Eq("gallery_id", galleryId)
                    ,
                    NHibernate.Expression.Expression.Eq("art_type_id", (int)typ)
                );
            NHibernateDao<art, int> artFactory = new NHibernateDao<art, int>();
            return artFactory.GetByCriteria(start, howMany, "date", false, criterion);
        }


        public int ReadCountArtByGalleryIdAndArtType(int galleryId, ContentNamesGlobals.ArtType typ)
        {
            NHibernate.Expression.ICriterion criterion =
                 NHibernate.Expression.Expression.And
                 (
                     NHibernate.Expression.Expression.Eq("gallery_id", galleryId)
                     ,
                     NHibernate.Expression.Expression.Eq("art_type_id", (int)typ)
                 );

            NHibernateDao<art, int> artFactory = new NHibernateDao<art, int>();
            return 
                artFactory.GetByCriteria(criterion).Count;
        }

        public bool HasGalleryAnyArt(int id)
        {
            NHibernateDao<art, int> artFactory = new NHibernateDao<art, int>();
            List<art> a = artFactory.GetByCriteria(NHibernate.Expression.Expression.Eq("gallery_id", id));
            if ((a != null) && (a.Count > 0))
                return true;
            return false;
        }

        public void DeleteGalleryById(int id)
        {
            NHibernateDao<gallery, int> galleryFactory = new NHibernateDao<gallery, int>();
            gallery g = galleryFactory.GetById(id, false);
            galleryFactory.Delete(g);
        }




    }
}
